/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package models.view;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import models.dao.ProductoDao;

/**
 *
 * @author cepardov
 */
public class Compras extends Proveedor {
    ProductoDao productodao=new ProductoDao();
//    Object [][] dttipo;//variable ya esta definida en el extends
//    int fila, idtipo=0;//variable ya esta definida en el extends
    /**
     * Creates new form IngresoProductos
     */
    public Compras() {
        initComponents();
        this.txtEnabled(false);//Bloquea los botone que no se pueden utilizar sin buscar un proveedor
        this.btnEnabled(false);
        this.inicializaForm();
//        this.updateTabla();
    }
    
    private void buscarProducto(){
        String prod_cod=this.txtCodProducto.getText();
        if(!prod_cod.isEmpty()){
            this.txtEnabled(true);
            this.btnAgregar.setEnabled(true);
            this.btnTerminar.setEnabled(true);
        }
    }
    
    private void proveedorTrue(boolean enabled){
        this.txtNumeroFactura.setEnabled(enabled);
        this.jdcFecha.setEnabled(enabled);
        this.txtCodProducto.setEnabled(enabled);
        this.btnBuscarProducto.setEnabled(enabled);
        this.btnTerminar.setEnabled(enabled);
    }
    
    private void inicializaForm(){
        this.txtEmpresa.setText("");
        this.txtNumeroFactura.setText("");
        this.txtCodProducto.setText("");
        this.txtProductoId.setText("");
        this.txtStock.setText("");
        this.txtNombreProducto.setText("");
        this.txtCantidad.setText("");
        this.cbUnidadMedida.setSelectedItem("Seleecione");
        this.txtDescripcionProducto.setText("");
        this.txtValorCosto.setText("");
        this.txtValorPrublico.setText("");
        this.txtUtilidades.setText("");
        
        this.chkbDescuento.setSelected(false);
        this.spnDescuento.setValue(1);
        this.chkbGanacia.setSelected(false);
        this.spnGanancia.setValue(1);
        this.chkbVencimiento.setSelected(false);
    }
    
    private void btnEnabled(boolean enabled){
        this.btnAgregar.setEnabled(enabled);
        this.btnQuitar.setEnabled(enabled);
        this.btnTerminar.setEnabled(enabled);
        this.btnGuardar.setEnabled(enabled);
        this.btnModificarProducto.setEnabled(enabled);
        this.btnBuscarProducto.setEnabled(enabled);
    }
    
    private void txtEnabled(boolean enabled){
        this.txtNumeroFactura.setEnabled(enabled);
        this.jdcFecha.setEnabled(enabled);
        this.txtCodProducto.setEnabled(enabled);
        this.txtProductoId.setEnabled(enabled);
        this.txtStock.setEnabled(enabled);
        this.txtNombreProducto.setEnabled(enabled);
        this.txtCantidad.setEnabled(enabled);
        this.chkbVencimiento.setEnabled(enabled);
        this.cbUnidadMedida.setEnabled(enabled);
        this.txtDescripcionProducto.setEnabled(enabled);
        this.txtValorCosto.setEnabled(enabled);
        this.chkbGanacia.setEnabled(enabled);
        this.txtValorPrublico.setEnabled(enabled);
        this.chkbDescuento.setEnabled(enabled);
        this.txtUtilidades.setEnabled(enabled);
        this.pnlListaproductos.setVisible(enabled);
    }
    
    private void updateTabla(){
        String[] columNames = {"RUT","Razon Social","Nombre Vendedor","Apellido Vendedor","Teléfonos","prov_correo","prov_descripcion"};  
        dttipo = productodao.getUsuarios();
        DefaultTableModel datos = new DefaultTableModel(dttipo,columNames);                      
        tblFactura.setModel(datos);
        //Autoredimensionar Columnas
//        try{
//            String col0=String.valueOf(tblFactura.getValueAt(0, 0));
//            int width=col0.length()*8;
//            System.out.println("Debug: Redimensión col0="+col0);
//            tblFactura.getColumnModel().getColumn(0).setMaxWidth(width);
//            tblFactura.getColumnModel().getColumn(0).setMinWidth(width);
//            tblFactura.getColumnModel().getColumn(0).setPreferredWidth(width);
//        } catch (Exception se){
//            System.out.println("Debug: Error redimensionar col0: "+se.getMessage());
//        }
//        //Esconder Comunas
//        tblFactura.getColumnModel().getColumn(6).setMaxWidth(0);
//        tblFactura.getColumnModel().getColumn(6).setMinWidth(0);
//        tblFactura.getColumnModel().getColumn(6).setPreferredWidth(0);
//        tblFactura.getColumnModel().getColumn(5).setMaxWidth(0);
//        tblFactura.getColumnModel().getColumn(5).setMinWidth(0);
//        tblFactura.getColumnModel().getColumn(5).setPreferredWidth(0);
    }
    
    private boolean buscarProveedor(){
        proveedoresbeans.setProv_id(prov_id);
        if(proveedoresbeans.find()){
            this.txtRutProveedor.setText(proveedoresbeans.getProv_id());
            this.txtEmpresa.setText(proveedoresbeans.getProv_empresa());
            this.proveedorTrue(true);
            this.txtCodProducto.requestFocus();
            return true;
        } else {
            this.txtEmpresa.setText("");
            this.txtEnabled(false);
            this.btnEnabled(false);
            JOptionPane.showMessageDialog(null,"El RUT Proveedor \""+prov_id+"\" no ha sido encontrado, revise si lo ha escrito correctamente \n"
                    + "de lo contrario debe ingresarlo en formulario de proveedores.","Busqueda de proveedores", JOptionPane.WARNING_MESSAGE);
            return false;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtRutProveedor = new javax.swing.JFormattedTextField();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtEmpresa = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtNumeroFactura = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jdcFecha = new datechooser.beans.DateChooserCombo();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtCodProducto = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtProductoId = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtStock = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtNombreProducto = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        chkbVencimiento = new javax.swing.JCheckBox();
        jdcVencimiento = new datechooser.beans.DateChooserCombo();
        jLabel10 = new javax.swing.JLabel();
        txtDescripcionProducto = new javax.swing.JTextField();
        cbUnidadMedida = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtValorCosto = new javax.swing.JTextField();
        chkbGanacia = new javax.swing.JCheckBox();
        jLabel13 = new javax.swing.JLabel();
        txtValorPrublico = new javax.swing.JTextField();
        chkbDescuento = new javax.swing.JCheckBox();
        spnDescuento = new javax.swing.JSpinner();
        spnGanancia = new javax.swing.JSpinner();
        jLabel21 = new javax.swing.JLabel();
        txtUtilidades = new javax.swing.JTextField();
        pnlListaproductos = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblFactura = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        btnCerrar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        btnModificarProducto = new javax.swing.JButton();
        btnBuscarProducto = new javax.swing.JButton();
        btnTerminar = new javax.swing.JButton();
        btnAgregar = new javax.swing.JButton();
        btnQuitar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();

        setTitle("Compras Por Factura");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del Proveedor"));
        jPanel1.setToolTipText("");

        jLabel1.setText("RUT");

        try {
            txtRutProveedor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###-A")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jButton2.setText("Buscar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel2.setText("Razón Social del Proveedor");

        txtEmpresa.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtEmpresa)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtRutProveedor))
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addGap(0, 64, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtRutProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de la Factura"));

        jLabel3.setText("N° Fac.:");

        txtNumeroFactura.setText("0");

        jLabel4.setText("Fecha");

        jdcFecha.setFormat(1);
        jdcFecha.setCurrentNavigateIndex(0);
        jdcFecha.setBehavior(datechooser.model.multiple.MultyModelBehavior.SELECT_SINGLE);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNumeroFactura, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jdcFecha, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumeroFactura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jdcFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de Producto a agregar"));

        jLabel5.setText("Código");

        jLabel6.setText("Producto ID");

        jLabel7.setText("Stock Actual");

        txtStock.setText("0");

        jLabel8.setText("Nombre");

        jLabel9.setText("Unid. Medida");

        chkbVencimiento.setText("Vencimiento");
        chkbVencimiento.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                chkbVencimientoItemStateChanged(evt);
            }
        });

        jdcVencimiento.setCurrentView(new datechooser.view.appearance.AppearancesList("Swing",
            new datechooser.view.appearance.ViewAppearance("custom",
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Ubuntu", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(85, 85, 85),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Ubuntu", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(85, 85, 85),
                    new java.awt.Color(0, 0, 255),
                    true,
                    true,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Ubuntu", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(0, 0, 255),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Ubuntu", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(128, 128, 128),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.LabelPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Ubuntu", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(85, 85, 85),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.LabelPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Ubuntu", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(85, 85, 85),
                    new java.awt.Color(255, 0, 0),
                    false,
                    false,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                (datechooser.view.BackRenderer)null,
                false,
                true)));
    jdcVencimiento.setEnabled(false);

    jLabel10.setText("Descripción");

    cbUnidadMedida.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));

    jLabel11.setText("Cantidad");

    txtCantidad.setText("0");

    jLabel12.setText("Valor Proveedor");

    txtValorCosto.setText("0");

    chkbGanacia.setText("% Ganancia");
    chkbGanacia.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            chkbGanaciaItemStateChanged(evt);
        }
    });

    jLabel13.setText("Valor Publico");

    txtValorPrublico.setText("0");

    chkbDescuento.setText("% Descuento Promocional");
    chkbDescuento.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            chkbDescuentoItemStateChanged(evt);
        }
    });

    spnDescuento.setModel(new javax.swing.SpinnerNumberModel(1, 1, 100, 1));
    spnDescuento.setEnabled(false);

    spnGanancia.setModel(new javax.swing.SpinnerNumberModel(1, 1, 100, 1));
    spnGanancia.setEnabled(false);

    jLabel21.setText("Utilidades");

    txtUtilidades.setText("0");

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addComponent(jLabel10)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(txtDescripcionProducto))
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addComponent(chkbVencimiento)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jdcVencimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel9)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(cbUnidadMedida, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jLabel8))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                    .addComponent(txtCodProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel6)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txtProductoId, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(txtNombreProducto))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel7)
                                .addComponent(jLabel11))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtStock, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                                .addComponent(txtCantidad)))
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(jLabel12)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtValorCosto, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(chkbGanacia)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(spnGanancia, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel13)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtValorPrublico, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(chkbDescuento)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(spnDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel21)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtUtilidades, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(0, 0, Short.MAX_VALUE)))
            .addContainerGap())
    );
    jPanel3Layout.setVerticalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel5)
                .addComponent(txtCodProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel6)
                .addComponent(txtProductoId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel7)
                .addComponent(txtStock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel8)
                .addComponent(txtNombreProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel11)
                .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(jdcVencimiento, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbUnidadMedida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(chkbVencimiento, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel10)
                .addComponent(txtDescripcionProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel12)
                .addComponent(txtValorCosto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(chkbGanacia)
                .addComponent(jLabel13)
                .addComponent(txtValorPrublico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(spnGanancia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(chkbDescuento)
                .addComponent(spnDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel21)
                .addComponent(txtUtilidades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(18, Short.MAX_VALUE))
    );

    pnlListaproductos.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalle Productos a Ingresar"));

    tblFactura.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null}
        },
        new String [] {
            "Title 1", "Title 2", "Title 3", "Title 4"
        }
    ));
    tblFactura.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            tblFacturaMouseClicked(evt);
        }
    });
    jScrollPane1.setViewportView(tblFactura);

    javax.swing.GroupLayout pnlListaproductosLayout = new javax.swing.GroupLayout(pnlListaproductos);
    pnlListaproductos.setLayout(pnlListaproductosLayout);
    pnlListaproductosLayout.setHorizontalGroup(
        pnlListaproductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1)
    );
    pnlListaproductosLayout.setVerticalGroup(
        pnlListaproductosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
    );

    jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Funciones"));

    btnCerrar.setText("Cerrar");
    btnCerrar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCerrarActionPerformed(evt);
        }
    });

    btnGuardar.setText("Guardar Compra");

    btnModificarProducto.setText("Modificar Producto");

    btnBuscarProducto.setText("Buscar Producto");
    btnBuscarProducto.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnBuscarProductoActionPerformed(evt);
        }
    });

    btnTerminar.setText("Cancelar");
    btnTerminar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnTerminarActionPerformed(evt);
        }
    });

    btnAgregar.setText("Agregar Producto");
    btnAgregar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnAgregarActionPerformed(evt);
        }
    });

    btnQuitar.setText("Quitar Producto");

    javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
    jPanel5.setLayout(jPanel5Layout);
    jPanel5Layout.setHorizontalGroup(
        jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel5Layout.createSequentialGroup()
            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jSeparator2))
                .addGroup(jPanel5Layout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnQuitar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAgregar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btnCerrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnTerminar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnModificarProducto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                        .addComponent(btnBuscarProducto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addContainerGap())
    );
    jPanel5Layout.setVerticalGroup(
        jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
            .addComponent(btnBuscarProducto)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btnAgregar)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btnModificarProducto)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btnQuitar)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
            .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btnGuardar)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btnTerminar)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btnCerrar)
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(pnlListaproductos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(15, 15, 15)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(0, 0, 0)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlListaproductos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    boolean chkbVencimientoState;
    private void chkbVencimientoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_chkbVencimientoItemStateChanged
        // TODO add your handling code here:
        this.chkbVencimientoState=this.chkbVencimiento.isSelected();
        System.out.println("[Debug] chkbVencimientoState="+chkbVencimientoState);
        if(this.chkbVencimiento.isSelected()){
            this.jdcVencimiento.setEnabled(true);
        } else {
            this.jdcVencimiento.setEnabled(false);
        }
    }//GEN-LAST:event_chkbVencimientoItemStateChanged

    boolean chkbGananciaSate;
    private void chkbGanaciaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_chkbGanaciaItemStateChanged
        // TODO add your handling code here:
        this.chkbGananciaSate=this.chkbGanacia.isSelected();
        System.out.println("[Debug] chkbGananciaSate="+chkbGananciaSate);
        if(this.chkbGanacia.isSelected()){
            this.spnGanancia.setEnabled(true);
        } else {
            this.spnGanancia.setEnabled(false);
        }
    }//GEN-LAST:event_chkbGanaciaItemStateChanged

    boolean chkbDescuentoState;
    private void chkbDescuentoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_chkbDescuentoItemStateChanged
        // TODO add your handling code here:
        this.chkbDescuentoState=this.chkbDescuento.isSelected();
        System.out.println("[Debug] chkbDescuentoState="+chkbDescuentoState);
        if(this.chkbDescuento.isSelected()){
            this.spnDescuento.setEnabled(true);
        } else {
            this.spnDescuento.setEnabled(false);
        }
    }//GEN-LAST:event_chkbDescuentoItemStateChanged

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.prov_id=this.txtRutProveedor.getText();
        this.buscarProveedor();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void tblFacturaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblFacturaMouseClicked
        // TODO add your handling code here:
        fila = tblFactura.rowAtPoint(evt.getPoint());
        if (fila > -1){
//            this.txtRut.setText(String.valueOf(tblFactura.getValueAt(fila, 0)));
//            this.txtEmpresa.setText(String.valueOf(tblFactura.getValueAt(fila, 1)));
//            this.txtNombre.setText(String.valueOf(tblFactura.getValueAt(fila, 2)));
//            this.txtApellidos.setText(String.valueOf(tblFactura.getValueAt(fila, 3)));
//            this.txtTelefono.setText(String.valueOf(tblFactura.getValueAt(fila, 4)));
//            this.txtCorreo.setText(String.valueOf(tblFactura.getValueAt(fila, 5)));
//            this.txtDescripcion.setText(String.valueOf(tblFactura.getValueAt(fila, 6)));
////            this.resetEstado();
        }
//        this.validarDatos("setVariables");
        this.btnGuardar.setEnabled(false);
        this.btnModificarProducto.setEnabled(true);
        this.btnBuscarProducto.setEnabled(true);
    }//GEN-LAST:event_tblFacturaMouseClicked

    private void btnTerminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTerminarActionPerformed
        // TODO add your handling code here:
        this.txtEnabled(false);
        this.btnEnabled(false);
        this.inicializaForm();
    }//GEN-LAST:event_btnTerminarActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        // TODO add your handling code here:
        this.btnGuardar.setEnabled(true);
        this.btnQuitar.setEnabled(true);
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnBuscarProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarProductoActionPerformed
        // TODO add your handling code here:
        this.buscarProducto();
    }//GEN-LAST:event_btnBuscarProductoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnBuscarProducto;
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnModificarProducto;
    private javax.swing.JButton btnQuitar;
    private javax.swing.JButton btnTerminar;
    private javax.swing.JComboBox cbUnidadMedida;
    private javax.swing.JCheckBox chkbDescuento;
    private javax.swing.JCheckBox chkbGanacia;
    private javax.swing.JCheckBox chkbVencimiento;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private datechooser.beans.DateChooserCombo jdcFecha;
    private datechooser.beans.DateChooserCombo jdcVencimiento;
    private javax.swing.JPanel pnlListaproductos;
    private javax.swing.JSpinner spnDescuento;
    private javax.swing.JSpinner spnGanancia;
    private javax.swing.JTable tblFactura;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtCodProducto;
    private javax.swing.JTextField txtDescripcionProducto;
    private javax.swing.JTextField txtEmpresa;
    private javax.swing.JTextField txtNombreProducto;
    private javax.swing.JTextField txtNumeroFactura;
    private javax.swing.JTextField txtProductoId;
    public javax.swing.JFormattedTextField txtRutProveedor;
    private javax.swing.JTextField txtStock;
    private javax.swing.JTextField txtUtilidades;
    private javax.swing.JTextField txtValorCosto;
    private javax.swing.JTextField txtValorPrublico;
    // End of variables declaration//GEN-END:variables
}
